<?php

namespace app\common\business\log;

use \think\Session;
use app\common\mysql\Users as usersMysql;
use app\common\business\user\Point as PointBn;
class Duiba extends \app\common\business\AbstractModel {

    /*
     * 记录兑吧的信息
     *
     * */

    public static function insertDuibaLog($params) {
        $duibainfo= usersMysql::name('duiba_order')->where(array('orderNum'=>$params['orderNum']))->find();
        if($duibainfo){
            exception("兑吧订单后重复", 100014);
        }
        $post['change_type']=2;
        $post['change_memo']='使用'.$params['credits'].'积分获得'.$params['description'];
        unset($params['change_type'],$params['change_memo']);
        usersMysql::startTrans();
        $params['add_time']=time();
        if (usersMysql::name('duiba_order')->insert($params)) {
            $post['point_type'] =3;
            $post['source_type']=0;
            $post['user_id'] =$params['user_id'];
            $post['point']=$params['credits'];
            $ret=PointBn::updatePoint($post);
            if (!$ret)
            usersMysql::rollback();
        }
        usersMysql::commit();
        return  true;;
    }
    /*
        * 记录兑吧的信息
        *
        * */

    public static function duibaInfo($params) {
        $where['duiba_id']  = $params['bizId'];
        $where['status'] = $params['status'];
        $data=usersMysql::name('duiba_order')->where($where)
        ->find();
        usersMysql::startTrans();
        if($data){
            $update['source_type']=0;
            $update['user_id']    =$data['user_id'];
            $update['point']      =$data['credits'];
            $update['change_type']=1;
            $update['change_memo']='商店兑换积分失败，返回积分';
            PointBn::updatePoint($update);
            unset($where['status']);
            if(!usersMysql::name('duiba_order')->where($where)->update(array('status'=>0)))
                usersMysql::rollback();
        }

        usersMysql::commit();
        return  true;;
    }
}
